package com.gxuwz.bignews.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gxuwz.bignews.MainActivity;
import com.gxuwz.bignews.R;
import com.gxuwz.bignews.controller.LoginController;
import com.gxuwz.bignews.model.entity.User;

public class LoginFragment extends Fragment {
    private EditText etPhone, etPassword;
    private Button btnLogin;
    private TextView tvToRegister, tvForgotPassword;
    private LoginController loginController;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        loginController = new LoginController(getContext());

        etPhone = view.findViewById(R.id.et_phone);
        etPassword = view.findViewById(R.id.et_password);
        btnLogin = view.findViewById(R.id.btn_login);
        tvToRegister = view.findViewById(R.id.tv_to_register);
        tvForgotPassword = view.findViewById(R.id.tv_forgot_password);

        btnLogin.setOnClickListener(v -> login());
        tvToRegister.setOnClickListener(v -> {
            if (getActivity() != null) {
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, new RegisterFragment())
                        .addToBackStack(null)
                        .commit();
            }
        });

        tvForgotPassword.setOnClickListener(v -> {
            if (getActivity() != null) {
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, new ForgotPasswordFragment())
                        .addToBackStack(null)
                        .commit();
            }
        });

        return view;
    }

    private void login() {
        String phone = etPhone.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (phone.isEmpty() || password.isEmpty()) {
            Toast.makeText(getContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = loginController.login(phone, password);
        if (user != null) {
            // 保存登录信息到SharedPreferences
            SharedPreferences sp = requireContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("phone", user.getPhone());
            editor.putString("username", user.getUsername());
            editor.apply();

            Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
            if (getActivity() instanceof MainActivity) {
                ((MainActivity) getActivity()).onLoginSuccess();
            }
        } else {
            Toast.makeText(getContext(), "手机号或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}